/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.HallRenting_Case;

import Com.Entity.Koneksi;
import Com.Entity.TransaksiGedung;
import com.toedter.calendar.JDateChooser;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Timer;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRTableModelDataSource;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author ASUS
 */
public class TransaksiGedungController {

    public static void showDateAndClock(final JLabel jamLabel, final JLabel tanggalLabel) {
        /**
         *
         * @author Anonim, via search google.com
         */
        ActionListener taskPerformer = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                GregorianCalendar gc = new GregorianCalendar();
                SimpleDateFormat frmtDate = new SimpleDateFormat("dd-MMM-yyyy");

                String nol_jam = "", nol_menit = "", nol_detik = "";

                java.util.Date dateTime = new java.util.Date();
                int nilai_jam = dateTime.getHours();
                int nilai_menit = dateTime.getMinutes();
                int nilai_detik = dateTime.getSeconds();

                if (nilai_jam <= 9) {
                    nol_jam = "0";
                }
                if (nilai_menit <= 9) {
                    nol_menit = "0";
                }
                if (nilai_detik <= 9) {
                    nol_detik = "0";
                }

                String jam = nol_jam + Integer.toString(nilai_jam);
                String menit = nol_menit + Integer.toString(nilai_menit);
                String detik = nol_detik + Integer.toString(nilai_detik);

                jamLabel.setText(jam + " : " + menit + " : " + detik + " WIB");
                tanggalLabel.setText(frmtDate.format(gc.getTime()));
            }
        };
        new Timer(1000, taskPerformer).start();
    }

    public static void simpanTransaksi(TransaksiGedung trans) throws SQLException {

        trans.simpanTransaksiGedung(trans);
    }

    public static String noTrans(TransaksiGedung trans) throws SQLException {
        return trans.callNoTransaksi();
    }

    public static int simpanButtonAction(JDateChooser tglLahirChooser1, JDateChooser tglSewaChooser, JLabel userText,
            JTextField namaTextField, JTextField noKTPTextField, JComboBox jenisIDCombo, JTextField noSewaTextField1,
            JComboBox jenisGedungCombo, JTextField jamSewaTextField, JTextField jamKeluarTextField, JTextField telpTextField,
            JTextField totaltext) throws Exception {
        int status = 0;
        try {
            // TODO add your handling code here:
            if (namaTextField.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Nama penyewa belum diisi");
                status = 0;
            } else if (noKTPTextField.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Nomor ID belum diisi");
                status = 0;
            } else if (jenisIDCombo.getSelectedIndex() == 0) {
                JOptionPane.showMessageDialog(null, "Jenis ID belum diisi");
                status = 0;
            } else if (telpTextField.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Nomor telepon belum diisi");
                status = 0;
            } else if (tglLahirChooser1.getDate() == null) {
                JOptionPane.showMessageDialog(null, "Tanggal lahir belum diisi");
                status = 0;
            } else if (tglSewaChooser.getDate() == null) {
                JOptionPane.showMessageDialog(null, "Tanggal sewa belum diisi");
                status = 0;
            } else if (jamSewaTextField.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Jam sewa belum diisi");
                status = 0;
            } else if (jamKeluarTextField.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Jam keluar belum diisi");
                status = 0;
            } else if (jenisGedungCombo.getSelectedIndex() == 0) {
                JOptionPane.showMessageDialog(null, "Jenis gedung belum dipilih");
                status = 0;
            } else {
                TransaksiGedungController call = new TransaksiGedungController();
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
                String tgllhr = format.format(tglLahirChooser1.getDate());
                String tglSewa = format.format(tglSewaChooser.getDate());
                TransaksiGedung gedung = TransaksiGedung.getKoneksiKontrol();
                gedung.setId_karyawan(userText.getText());
                gedung.setNama_penyewa(namaTextField.getText());
                gedung.setIdPenyewa(noKTPTextField.getText());
                gedung.setJenisId(jenisIDCombo.getSelectedItem().toString());
                gedung.setNo_sewa(noSewaTextField1.getText());
                gedung.setJenisGedung(jenisGedungCombo.getSelectedItem().toString().toUpperCase());
                gedung.setJamSewa(jamSewaTextField.getText());
                gedung.setJamKeluar(jamKeluarTextField.getText());
                gedung.setNo_telp(telpTextField.getText());
                gedung.setTanggalLahir(tgllhr);
                gedung.setTanggalSewa(tglSewa);
                // if(gedung.setTotalHarga(Long.parseLong(totaltext.getText())== " ")){
                if (totaltext.getText().matches("")) {
                    gedung.setTotalHarga(Long.parseLong("0"));
                } else {
                    gedung.setTotalHarga(Long.parseLong(totaltext.getText()));
                }

                gedung.simpanTransaksiGedung(gedung);
//                JOptionPane.showMessageDialog(null, "" + namaTextField.getText());
//                JOptionPane.showMessageDialog(null, "" + noKTPTextField.getText());
//                JOptionPane.showMessageDialog(null, "" + jenisIDCombo.getSelectedItem().toString());
                cetakBukti(gedung.getNo_sewa());
                status = 1;
            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Data Gagal Disimpan");

        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Data Gagal Disimpan");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Data Gagal Disimpan");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        return status;
    }

    public static void exit(javax.swing.JDialog dialog, String noTrans) {
        try {
            TransaksiGedung.getKoneksiKontrol().batalTrans(noTrans);
            dialog.dispose();
        } catch (Exception ex) {
            dialog.dispose();
        }

    }

    public static void hitungTotal(JTextField jamSewaTextField, JTextField jamKeluarTextField,
            JTextField totaltext, JComboBox jenisGedungCombo, javax.swing.JDialog dialog) {
        if (jenisGedungCombo.getSelectedIndex() != 0) {
            try {
                String masuk = jamSewaTextField.getText();
                String keluar = jamKeluarTextField.getText();
                TransaksiGedung tg = new TransaksiGedung();
                tg.setJamSewa(masuk);
                tg.setJamKeluar(keluar);
                if (!masuk.matches("") && !keluar.matches("")) {
                    int jamMasuk = Integer.parseInt(masuk.substring(0, 2));
                    int menitMasuk = Integer.parseInt(masuk.substring(3, 5));
                    int jamKeluar = Integer.parseInt(keluar.substring(0, 2));
                    int menitKeluar = Integer.parseInt(keluar.substring(3, 5));
                    int waktuMasuk = (jamMasuk * 60) + menitMasuk;
                    int waktuKeluar = (jamKeluar * 60) + menitKeluar;
                    long harga = 0;
                    String koma = String.valueOf(((double) (waktuKeluar - waktuMasuk)) / 60);
                    int loc = koma.lastIndexOf('.');
                    int lamaSewa = 0;
                    int decimal = Integer.parseInt(koma.substring(loc + 1, loc + 2));
                    if (decimal > 5) {
                        lamaSewa = ((waktuKeluar - waktuMasuk) / 60) + 1;
                    } else {
                        lamaSewa = ((waktuKeluar - waktuMasuk) / 60);
                    }
                    if (waktuKeluar < waktuMasuk) {
                        jenisGedungCombo.setSelectedIndex(0);
                        JOptionPane.showMessageDialog(dialog.getRootPane(), "Waktu keluar lebih kecil dari waktu sewa");
                    } else {
                        if (jenisGedungCombo.getSelectedIndex() == 1) {
                            harga = (long) lamaSewa * 600000;
                        } else if (jenisGedungCombo.getSelectedIndex() == 2) {
                            harga = (long) lamaSewa * 400000;
                        }
                    }
                    totaltext.setText(String.valueOf(harga));
                } else {
                    totaltext.setText(String.valueOf(0));
                }
            } catch (Exception ex) {
                jenisGedungCombo.setSelectedIndex(0);
                JOptionPane.showMessageDialog(dialog.getRootPane(), ex.getMessage());
            }
        }
    }

    public static void cetakBukti(String noTransaksi) {
        try {
            Connection koneksi = Koneksi.getDBConnection();
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("notransaksigedung", noTransaksi);
            JasperPrint jasperPrint = JasperFillManager.fillReport("./Reports/notagedung.jasper",
                    params, koneksi);
            JasperViewer.viewReport(jasperPrint, false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Reports Error  " + e.toString());
        }
    }
}
